<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="等额本息" name="first" />
          <el-tab-pane label="等额本金" name="second" />
        </el-tabs>
        <!-- 搜索 -->
        <label class="el-form-item-label">贷款总额</label>
        <el-input v-model="loanAmount" clearable placeholder="贷款总额" style="width: 125px;" class="filter-item" />
        <label class="el-form-item-label" style="margin-left:-10px">万</label>

        <label class="el-form-item-label">商贷利率</label>
        <el-input v-model="annualInterestRate" clearable placeholder="商贷利率" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label" style="margin-left:-10px">%</label>

        <label class="el-form-item-label">贷款年限</label>
        <el-input v-model="loanTermMonths" clearable placeholder="贷款年限" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label" style="margin-left:-10px">年</label>

        <el-button

          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-s-promotion"
          @click="calRepay"
        >
          计算
        </el-button>
        <!-- <el-input v-model="query.repayStatus" clearable placeholder="状态" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <!-- <rrOperation :crud="crud" /> -->
      </div>

      <el-table ref="table" v-loading="crud.loading" :data="rePayData" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <!-- <el-table-column prop="repayType" label="还款类型" /> -->
        <el-table-column prop="month" label="期数" align="center" />
        <el-table-column prop="monthlyPayment" label="月供总额" align="center" />
        <el-table-column prop="principalPayment" label="月供本金" align="center">
          <template slot-scope="scope">
            {{ scope.row.principalPayment.toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column prop="interestPayment" label="月供利息" align="center">
          <template slot-scope="scope">
            {{ scope.row.interestPayment.toFixed(2) }}
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudRepay from '@/api/repay'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import rrOperation from '@crud/RR.operation'
// import crudOperation from '@crud/CRUD.operation'
// import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
import { downloadFile } from '@/utils/index'
import { download } from '@/api/data'

const defaultForm = { id: null, repayType: null, repayIssue: null, repayDate: null, repayPrice: null, repayPrincipal: null, repayInterest: null, repayStatus: null }
export default {
  name: 'Repay',
  components: { pagination },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'repay', url: '', idField: 'id', sort: 'repayIssue,asc', crudMethod: { ...crudRepay }})
  },
  data() {
    return {
      page: {
      // 页码
        page: 0,
        // 每页数据条数
        size: 10,
        // 总数据条数
        total: 0
      },
      activeName: 'first',
      rePayData: [],
      loanTermMonths: null,
      annualInterestRate: null,
      loanAmount: null,
      status: [{ id: '1', label: '已还', value: '1' }, { id: '0', label: '待还', value: '0' }],
      headers: { 'Authorization': getToken() },
      permission: {
        add: ['admin', 'repay:add'],
        edit: ['admin', 'repay:edit'],
        del: ['admin', 'repay:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'repayType', display_name: '还款类型' },
        { key: 'repayStatus', display_name: '状态' }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'baseApi',
      'fileUploadApi'
    ])
  },
  methods: {
    handleClick(tab) {
      this.activeName = tab.name
    },
    calRepay() {
      if (this.loanAmount === null && this.annualInterestRate === null && this.loanTermMonths === null) {
        this.$message.error('请输入查询的数据')
      } else {
        if (this.activeName === 'first') {
          crudRepay.getCalculateEqualPrincipalAndInterest((this.loanAmount * 10000), (this.annualInterestRate * 0.01), (this.loanTermMonths * 12)).then(res => {
            this.rePayData = res
          })
        } else {
          crudRepay.getEqualPrincipal((this.loanAmount * 10000), (this.annualInterestRate * 0.01), (this.loanTermMonths * 12)).then(res => {
            this.rePayData = res
          })
        }
      }
    },
    beforeupload(file) {
      const isLt1M = file.size / 1024 / 1024 < 100
      if (!isLt1M) {
        this.$message.error('上传文件大小不能超过 100MB!')
      }
      this.fileName = file.name
      return isLt1M
    },
    /** 导入模板的成功状态 */
    handleSuccessFile(res, file) {
      if (res.state === 'success') {
        this.$message({
          message: res.info,
          type: 'success'
        })
        this.crud.toQuery()
      } else {
        this.$message.error(res.message)
      }
    },
    /** 失败状态 */
    handleErrorFile(file) {
      this.$message.error('数据导入失败！')
    },
    /** 下载模板 */
    downloadModel() {
      var params = { }
      params.id = -1
      download(this.crud.url + '/export', params).then(result => {
        downloadFile(result, this.crud.title + '模板', 'xlsx')
      })
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
